#include"../Server/Rpc_server.hpp"

bool Add(const Json::Value &req, Json::Value &rsp)
{
    int num1 = req["num1"].asInt();
    int num2 = req["num2"].asInt();
    rsp["result"]=num1+num2;
    return true;
}

bool Gamer(const Json::Value &params, Json::Value &result)
{
    string game=params["game"].asString();
    if(game=="原神")
        result["正在打开"]="原神启动";
    else if(game=="魔兽世界")
        result["正在打开"]="魔兽世界启动";
    else if( game=="第五人格")
        result["正在打开"]="第五人格启动";
    else result["正在打开"]="游戏不存在";
        return true;
}

int main()
{
    zgwrpc::Dispatcher::ptr dispatcher=zgwrpc::DispatcherFactory::create();

    auto router=make_shared<zgwrpc::server::Rpc_Router>();
    auto cb=std::bind(&zgwrpc::server::Rpc_Router::OnRpcRequest,router.get(),placeholders::_1,placeholders::_2);
    
    zgwrpc::server::ServerDescribeBuilder buiderAdd;
    buiderAdd.SetCallback(Add);
    buiderAdd.SetMthodName("Add");
    buiderAdd.SetReturnType(zgwrpc::server::VType::INTEGRAL);
    buiderAdd.SetParamsDescribe("num1", zgwrpc::server::VType::INTEGRAL);
    buiderAdd.SetParamsDescribe("num2", zgwrpc::server::VType::INTEGRAL);

    zgwrpc::server::ServerDescribeBuilder buiderGame;
    buiderGame.SetCallback(Gamer);
    buiderGame.SetMthodName("Game");
    buiderGame.SetReturnType(zgwrpc::server::VType::STRING);
    buiderGame.SetParamsDescribe("game", zgwrpc::server::VType::STRING);


    shared_ptr<zgwrpc::server::ServerDescribeBuilder> rpcserver_builder=make_shared<zgwrpc::server::ServerDescribeBuilder>();
    rpcserver_builder->SetMthodName("Add");
    rpcserver_builder->SetParamsDescribe("num1",zgwrpc::server::VType::INTEGRAL);
    rpcserver_builder->SetParamsDescribe("num2",zgwrpc::server::VType::INTEGRAL);
    rpcserver_builder->SetReturnType(zgwrpc::server::VType::INTEGRAL);
    rpcserver_builder->SetCallback(Add);

    zgwrpc::Address a1=make_pair("127.0.0.1",9090);
    zgwrpc::Address a2=make_pair("127.0.0.1",8080);
    zgwrpc::server::Rpc_Server server(a1,a2,true);
    server.Register(rpcserver_builder->Build());
    server.Register(buiderAdd.Build());
    server.Register(buiderGame.Build());
    cout<<"server start"<<endl;
    server.start();

    return 0;

}